using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class GameManager : MonoBehaviour
{
    public static GameManager instance;
    [Header("GameObject")]
    public EnemyPool enemypool;
    public WeaponPool weaponPool;
    public Player player;
    public LevelUp levelUpUI;
    public Result resultUI;
    public GameObject enemyCleaner;
    [Header("GameControl")]
    public float gameTime;
    public float maxGameTime = 50f;
    public bool isLive = true;
    [Header("PlayerInfo")]
    public int playerID;
    public int level;
    public int kill;
    public int exp;
    public float health;
    public float maxHealth = 100;
    public int[] nextExp = {10,30,60,100,150, 200,400,600 };

    private void Awake()
    {
        instance = this;
    }
    // Start is called before the first frame update
    void Start()
    {
        //health = maxHealth;
        //levelUpUI.Select(0);
    }

    // Update is called once per frame
    void Update()
    {
        if (isLive)
        {
            gameTime += Time.deltaTime;
            if (gameTime > maxGameTime)
            {
                gameTime = maxGameTime;
                GameVictory();
            }
        } 
    }

    public void GetExp(int takeExp)
    {
        if (isLive)
        {
            exp += takeExp;
            if (exp >= nextExp[Mathf.Min(level, nextExp.Length - 1)])
            {
                level++;
                exp = 0;
                levelUpUI.Show();
            }
        }
    }

    public void Stop()
    {
        isLive = false;
        Time.timeScale = 0;
    }

    public void Resume()
    {
        isLive = true;
        Time.timeScale = 1;
    }

    public void GameStart(int id)
    {
        playerID = id;
        health = maxHealth;
        player.gameObject.SetActive(true);
        levelUpUI.Select(playerID % 2);
        Resume();
        AudioManager.instance.PlayBgm(true);
        AudioManager.instance.PlaySfx(AudioManager.Sfx.Select);
    }

    public void GameRestart()
    {
        SceneManager.LoadScene(0);
    }

    public void GameOver()
    {
        StartCoroutine(GameOverRoutine());
    }

    IEnumerator GameOverRoutine()
    {
        isLive = false;
        yield return new WaitForSeconds(0.5f);
        resultUI.gameObject.SetActive(true);
        resultUI.Lose();
        Stop();
        AudioManager.instance.PlayBgm(false);
        AudioManager.instance.PlaySfx(AudioManager.Sfx.Lose);

    }

    public void GameVictory()
    {
        StartCoroutine(GameVictoryRoutine());
    }

    public void GameQuit()
    {
        Application.Quit();
    }

    IEnumerator GameVictoryRoutine()
    {
        isLive = false;
        enemyCleaner.SetActive(true);
        yield return new WaitForSeconds(0.5f);
        resultUI.gameObject.SetActive(true);
        resultUI.Win();
        Stop();
        AudioManager.instance.PlayBgm(false);
        AudioManager.instance.PlaySfx(AudioManager.Sfx.Win);
    }
}

